<template>
  <div class="login-account">
    <el-form
      ref="accountFormRef"
      :model="accountFormModal"
      :rules="rules"
      label-width="60px"
    >
      <el-form-item label="账号" prop="name">
        <el-input
          v-model="accountFormModal.name"
          placeholder="请输入账号"
        ></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input
          v-model="accountFormModal.password"
          type="password"
          placeholder="请输入密码"
        ></el-input>
      </el-form-item>
    </el-form>
  </div>
</template>

<script lang="ts">
import { defineComponent, reactive, ref } from 'vue'
import { useStore } from 'vuex'
import { ElForm } from 'element-plus'
import storage from '@/utils/storage'
export default defineComponent({
  name: 'LoginAccount',
  setup() {
    const accountFormModal = reactive({
      name: storage.getStorage('accountName') || '',
      password: storage.getStorage('accountPassword') || ''
    })
    const rules = {
      name: [
        {
          required: true,
          message: '请输入账号',
          trigger: 'blur'
        }
      ],
      password: [
        {
          required: true,
          message: '请输入密码',
          trigger: 'blur'
        }
      ]
    }
    const store = useStore()
    const accountFormRef = ref<InstanceType<typeof ElForm>>()
    const checkAccount = (isPass: boolean) => {
      accountFormRef.value?.validate((valid) => {
        if (valid) {
          if (isPass) {
            // 需要记住密码
            storage.setStorage('accountName', accountFormModal.name)
            storage.setStorage('accountPassword', accountFormModal.password)
          } else {
            // 不需要记住密码
            storage.removeStorage('accountName')
            storage.removeStorage('accountPassword')
          }

          store.dispatch('login/accountLoginAction', { ...accountFormModal })
        }
      })
    }
    return {
      accountFormModal,
      rules,
      accountFormRef,
      checkAccount
    }
  }
})
</script>

<style lang="less" scoped></style>
